package com.example.studyFile.jdknewfeatures._01_CompletableFuture_supplyAsync;

import com.example.pojo.User;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * @author yangjicong
 * @date 2022/1/2
 * @description: 有返回值的异步任务
 */
@Slf4j
public class completableFuture1 {
	public static void main(String[] args) throws ExecutionException, InterruptedException {

		ArrayList<User> users = new ArrayList<>();

		//创建异步任务
		CompletableFuture<Void> async = CompletableFuture.supplyAsync(()->getUser(),
				ThreadPoolManagerTwo.THREAD_POOL_EXECUTOR).thenAccept(res->{
					users.add(res);
		});

		System.out.println("集合中的结果 = " + users);
		System.out.println("异步执行的结果->"+async.get());
		System.out.println("主线程开始时间->"+System.currentTimeMillis());
		//等待子任务执行完成
		//有了异步回调方法thenAccept之后，就过去不到返回值了。
		System.out.println("主线程结束时间->"+System.currentTimeMillis());
	}

	public static User getUser(){
		User user = new User();
		user.setId(1L);
		user.setAddedTime("123");
		user.setFullName("好好");
		user.setUserType("VIP");

		System.out.println("异步线程结束时间->"+System.currentTimeMillis());
		return user;
	}
}
